package com.sky.wsp.rabbitmq.topic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @desccription Topic类型MQ配置
 * @auth wangshaopeng@talkweb.com.cn
 * @date 2022/9/29
 */
@Configuration
public class TopicMqConfig {
    // ------------------------ IDaaS事件交换机 ------------------------
    public static final String USER_PASSWORD_TOPIC_EXCHANGE = "idaas.user.password.topic";

    @Bean
    TopicExchange userPasswordTopicExchange() {
        return new TopicExchange(USER_PASSWORD_TOPIC_EXCHANGE);
    }

    // ------------------------ 消费者1 ------------------------
    public static final String IDAAS_USER_PASSWORD_QUEUE = "idaas.user.password";

    @Bean
    public Queue userPasswordQueue() {
        return new Queue(IDAAS_USER_PASSWORD_QUEUE);
    }

    // ------------------------ 消费者2 ------------------------
    public static final String IDAAS_USER_PASSWORD_QUEUE_2 = "idaas.user.password2";

    @Bean
    public Queue userPasswordQueue2() {
        return new Queue(IDAAS_USER_PASSWORD_QUEUE_2);
    }

    // ------------------------ 消息队列绑定到交换机 ------------------------
    /** 路由键，即租户ID，保证只监听本租户下的消息，三方应用要使用自己的租户ID */
    public static final String ROUTING_KEY = "defaultTenantId";

    @Bean
    Binding userPasswordQueueBindingExchange1() {
        return BindingBuilder
                // 消息队列
                .bind(userPasswordQueue())
                // 绑定到交换机
                .to(userPasswordTopicExchange())
                // 使用的路由KEY
                .with(ROUTING_KEY);
    }

    @Bean
    Binding userPasswordQueueBindingExchange2() {
        return BindingBuilder
                // 消息队列
                .bind(userPasswordQueue2())
                // 绑定到交换机
                .to(userPasswordTopicExchange())
                // 使用的路由KEY
                .with(ROUTING_KEY);
    }
}